package com.example.xgakj.newterminal.ui.framents;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.xgakj.mylibrary.mvp.utils.CommonUtil;
import com.example.xgakj.mylibrary.mvp.utils.ToastUtil;
import com.example.xgakj.newterminal.R;
import com.example.xgakj.newterminal.base.BaseApplication;
import com.example.xgakj.newterminal.base.BaseFragment;
import com.example.xgakj.newterminal.models.QueryModel;
import com.example.xgakj.newterminal.ui.activities.AlarmSetActivity;
import com.example.xgakj.newterminal.ui.activities.DeviceManageActivity;
import com.example.xgakj.newterminal.ui.activities.MonitorManageActivity;
import com.example.xgakj.newterminal.ui.activities.UserInfoActivity;
import com.example.xgakj.newterminal.views.CircleMenuAdapter;
import com.example.xgakj.newterminal.views.CircleMenuGroup;
import com.example.xgakj.newterminal.views.CircleMenuItem;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author Administrator
 * @date 2017-10-18
 */

public class MainFragment extends BaseFragment {


    @BindView(R.id.circlemenu)
    CircleMenuGroup circleMenuGroup;
    @BindView(R.id.rl_content)
    RelativeLayout rlContent;
    private ImageView ivAlarm;
    private Unbinder unbinder;
    private List<CircleMenuItem> mMenuItems = new ArrayList<>();
    private String[] mItemTexts = new String[]{"实时视频", "个人信息", "系统设置",
            "记录查询","设备管理"};
    private int[] mItemImgs = new int[]{R.drawable.video,
            R.drawable.userinfo, R.drawable.set,
            R.drawable.record,R.drawable.shebei
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        initAdapterData(mItemTexts, mItemImgs);
        unbinder = ButterKnife.bind(this, rootView);
        View centerView = LayoutInflater.from(getActivity()).inflate(R.layout.circle_menu_item_center, null, false);
        circleMenuGroup.setAdapter(new CircleMenuAdapter(mMenuItems));
        circleMenuGroup.setCenterView(centerView);
        ivAlarm=centerView.findViewById(R.id.iv_alarm);
        ivAlarm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtil.isNull(BaseApplication.ssn)){
                    ToastUtil.showShort(getActivity(),"请登录！");
                    return;
                }
                QueryModel.getInstance().remoteAlarm(getActivity());
            }
        });
        circleMenuGroup.setOnMenuItemClickListener(new CircleMenuGroup.OnMenuItemClickListener() {
            @Override
            public void itemClick(View view, int pos) {
                switch (pos){
                    case 0:

                        break;
                    case 1:
                        if (CommonUtil.isNull(BaseApplication.ssn)){
                            ToastUtil.showShort(getActivity(),"请登录！");
                            return;
                        }
                        Intent intent1=new Intent(getActivity(), UserInfoActivity.class);
                        startActivity(intent1);
                        break;
                    case 2:
                        Intent intent2=new Intent(getActivity(), AlarmSetActivity.class);
                        startActivity(intent2);
                        break;
                    case 3:
                        if (CommonUtil.isNull(BaseApplication.ssn)){
                            ToastUtil.showShort(getActivity(),"请登录！");
                            return;
                        }
                        Intent intent3=new Intent(getActivity(), MonitorManageActivity.class);
                        startActivity(intent3);
                        break;
                    case 4:
                        if (CommonUtil.isNull(BaseApplication.ssn)){
                            ToastUtil.showShort(getActivity(),"请登录！");
                            return;
                        }
                        Intent intent4=new Intent(getActivity(), DeviceManageActivity.class);
                        startActivity(intent4);
                        break;

                    default:
                }
            }
        });


        return rootView;
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 初始化列表数据
     *
     * @param mItemTexts 标题
     * @param mItemImgs  图标
     */
    private void initAdapterData(String[] mItemTexts, int[] mItemImgs) {
        if (mItemImgs == null && mItemTexts == null) {
            throw new IllegalArgumentException("文本和图片不能为空");
        }
        int count = mItemImgs == null ? mItemTexts.length : mItemImgs.length;
        if (mItemImgs != null && mItemTexts != null) {
            count = Math.min(mItemImgs.length, mItemTexts.length);
        }

        for (int i = 0; i < count; i++) {
            mMenuItems.add(new CircleMenuItem(mItemImgs[i], mItemTexts[i]));
        }
    }


}
